package org.dtrd.modules.archive.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.archive.entity.bean.ReportInfo;
import org.dtrd.modules.archive.entity.po.DtrdEntRdArchiveReport;
import org.dtrd.modules.archive.mapper.DtrdEntRdArchiveReportMapper;
import org.dtrd.modules.archive.service.IDtrdEntRdArchiveReportService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 用户报告单-待归类分组报告 服务实现类
 * </p>
 *
 * @author xia
 * @since 2022-09-12
 */
@DS("multi-datasource1")
@Service
public class DtrdEntRdArchiveReportServiceImpl extends ServiceImpl<DtrdEntRdArchiveReportMapper, DtrdEntRdArchiveReport> implements IDtrdEntRdArchiveReportService {

    @Override
    public List<ReportInfo> getPatientReport(Integer patientId, Integer type) {
        LambdaQueryWrapper<DtrdEntRdArchiveReport> queryWrapper = baseQueryWrapper()
                .eq(patientId != null, DtrdEntRdArchiveReport::getPatientId, patientId)
                .eq(type != null, DtrdEntRdArchiveReport::getReportType, type)
                .orderByAsc(DtrdEntRdArchiveReport::getReportType).orderByDesc(DtrdEntRdArchiveReport::getCreateTime);
        List<DtrdEntRdArchiveReport> poList = list(queryWrapper);
        List<ReportInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream().map(ReportInfo::new).collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public boolean groupPatientReport(Integer patientId, List<Integer> reportIdList, Integer type) {
        List<DtrdEntRdArchiveReport> pos = baseMapper.selectBatchIds(reportIdList);
        if (!CollectionUtils.isEmpty(pos)) {
            pos.forEach(po -> po.setReportType(type));
            return saveOrUpdateBatch(pos);
        }
        return false;
    }

    @Override
    public boolean saveOrUpdateReport(ReportInfo dto) {
        Integer patientId = dto.getPatientId();
        List<DtrdEntRdArchiveReport> dataList = new ArrayList<>(dto.getReportUrls().size());
        dto.getReportUrls().stream().map(reportUrl -> {
            DtrdEntRdArchiveReport dtrdEntRdArchiveReport = new DtrdEntRdArchiveReport().init();
            dtrdEntRdArchiveReport.setPatientId(patientId);
            dtrdEntRdArchiveReport.setReportUrls(reportUrl);
            dtrdEntRdArchiveReport.setReportType(dto.getType());
            return dtrdEntRdArchiveReport;
        }).forEach(dataList::add);
        return saveBatch(dataList);
    }


    private LambdaQueryWrapper<DtrdEntRdArchiveReport> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdEntRdArchiveReport.class).eq(DtrdEntRdArchiveReport::getIsDel, 0);
    }

}
